<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选类型 -->
			<cl-filter label="类型">
				<cl-select prop="type" :options="options.type" />
			</cl-filter>
			<!-- 筛选状态 -->
			<cl-filter label="状态">
				<cl-select prop="status" :options="options.status" />
			</cl-filter>
			<!-- 筛选优先级 -->
			<cl-filter label="优先级">
				<cl-select prop="priority" :options="options.priority" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索标题、发送者、接收者" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="tasksystem-task_info" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, reactive, ref } from "vue";

const { service } = useCool();

// 选项
const options = reactive({
	type: [
		{ label: "普通", value: 0, type: "danger" },
		{ label: "紧急", value: 1, type: "success" }
	],
	status: [
		{ label: "系统通知", value: 0 },
		{ label: "支付通知", value: 1 }
	],
	priority: [
		{ label: "低", value: 0 },
		{ label: "中", value: 1 },
		{ label: "高", value: 2 }
	]
});
let userList = ref<any>([]);

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "标题",
			prop: "title",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "内容",
			prop: "content",
			component: { name: "el-input", props: { type: "textarea", rows: 4 } },
			required: true
		},
		{
			label: "类型",
			prop: "type",
			component: { name: "el-radio-group", options: options.type },
			value: 0,
			required: true
		},
		{
			label: "状态",
			prop: "status",
			component: { name: "el-radio-group", options: options.status },
			value: 0,
			required: true
		},
		{
			label: "发送者",
			prop: "sender",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "接收者",
			prop: "receiver",
			component: {
				name: "el-input-number",
				// options: userList,
				props: { clearable: true, filterable: true }
			},
			required: true
		},
		{
			label: "时间",
			prop: "time",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" }
			},
			required: true
		},
		{
			label: "优先级",
			prop: "priority",
			component: { name: "el-radio-group", options: options.priority },
			value: 0,
			required: true
		},
		{
			label: "附件",
			prop: "attachments",
			component: { name: "cl-upload", props: { type: "file", multiple: true } }
		},
		{
			label: "链接",
			prop: "link",
			component: { name: "el-input", props: { clearable: true } }
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "标题", prop: "title", minWidth: 140 },
		{ label: "内容", prop: "content", showOverflowTooltip: true, minWidth: 200 },
		{ label: "类型", prop: "type", dict: options.type, minWidth: 120 },
		{ label: "状态", prop: "status", dict: options.status, minWidth: 120 },
		{ label: "发送者", prop: "sender", minWidth: 140 },
		{ label: "接收者", prop: "receiver", dict: userList, minWidth: 140 },
		{
			label: "时间",
			prop: "time",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{ label: "优先级", prop: "priority", dict: options.priority, minWidth: 120 },
		{ label: "附件", prop: "attachments", minWidth: 120, component: { name: "cl-link" } },
		{ label: "链接", prop: "link", minWidth: 140 },
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.tasksystem.task_info
	},
	(app) => {
		// console.log(service.member.list(), "11");
		app.refresh();
	}
);

const userData = async () => {
	let data = await service.member.info.list();
	userList.value = data.map((o: any) => {
		return {
			value: o.id,
			label: o.username
		};
	});
};

onMounted(() => {
	// userData();
});

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
